//
//  ShareManager.m
//  YJAPPThird
//
//  Created by 张吉旭 on 16/12/20.
//  Copyright © 2016年 yj. All rights reserved.
//

#import "ShareManager.h"
#import <UMSocialCore/UMSocialCore.h>

static ShareManager * instance = nil;

@implementation ShareManager

+(ShareManager*)shareInstance{
    static dispatch_once_t once_Token;
    dispatch_once(&once_Token, ^{
        instance = [[ShareManager alloc] init];
    });
    
    return instance;
}

- (void)shareWithTag:(NSInteger)btnTag shareModel:(ShareModel*)shareModel{
    [self handleSharewithTitle:shareModel.title url:shareModel.url text:shareModel.text imageUrl:shareModel.imageUrl btnTag:btnTag];
}



- (void)handleSharewithTitle:(NSString *)title url:(NSString *)url text:(NSString *)text imageUrl:(NSString *)imageUrl btnTag:(NSInteger)btnTag {
    
    switch (btnTag) {
        case 100:{//微信朋友圈
            [self shareToType:UMSocialPlatformType_WechatTimeLine title:title text:text url:url imageUrl:imageUrl];
        }
            break;
            
        case 101:{//QQ
            [self shareToType:UMSocialPlatformType_QQ title:title text:text url:url imageUrl:imageUrl];
        }
            break;
            
        case 102:{//微信
            [self shareToType:UMSocialPlatformType_WechatSession title:title text:text url:url imageUrl:imageUrl];
        }
            break;
            
        default:{//新浪微博
            [self sinaShareWithtitle:title text:text url:url imageUrl:imageUrl];
        }
            break;
    }
    
    
}

- (void)sinaShareWithtitle:(NSString *)title text:(NSString *)text url:(NSString*)url imageUrl:(NSString *)imageUrl{
    //创建分享消息对象
    UMSocialMessageObject *messageObject = [UMSocialMessageObject messageObject];
    //设置文本
    messageObject.text = [NSString stringWithFormat:@"%@%@",title,url];
    //创建图片内容对象
    UMShareImageObject *shareObject = [[UMShareImageObject alloc] init];
    //如果有缩略图，则设置缩略图
    shareObject.thumbImage = imageUrl;
    [shareObject setShareImage:imageUrl];
    //分享消息对象设置分享内容对象
    messageObject.shareObject = shareObject;
    //调用分享接口
    [[UMSocialManager defaultManager] shareToPlatform:UMSocialPlatformType_Sina messageObject:messageObject currentViewController:self completion:^(id data, NSError *error) {
        
        NSString *message = nil;
        if (!error) {
            
            [self executeShareTask];
            
            message = [NSString stringWithFormat:@"分享成功"];
            // [UIAlertController alertControllerWithTitle:@"" message:message preferredStyle:UIAlertControllerStyleAlert];
            //UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"" message:message delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
            //[alert show];
            [self shareSuccessNotice];
        }
        
    }];
    
}

//微信、QQ分享平台
-(void)shareToType:(UMSocialPlatformType)platformType title:(NSString *)title text:(NSString *)text url:(NSString*)url imageUrl:(NSString *)imageUrl{
    
    // 分享数据对象
    UMSocialMessageObject *messageObject = [UMSocialMessageObject messageObject];
    //创建网页分享内容对象
    UMShareWebpageObject *shareObject = [UMShareWebpageObject shareObjectWithTitle:title descr:text thumImage:imageUrl];
    [shareObject setWebpageUrl:url];
    messageObject.shareObject = shareObject;
    
    [[UMSocialManager defaultManager] shareToPlatform:platformType messageObject:messageObject currentViewController:self completion:^(id data, NSError *error) {
        
        NSString *message = nil;
        if (!error) {
            
            [self executeShareTask];
            
            message = [NSString stringWithFormat:@"分享成功"];
            //UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"" message:message delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
            //[alert show];
            [self shareSuccessNotice];
        }else{
            NSLog(@"失败");
        }
        
    }];
    
}

//分享成功后通知
-(void)shareSuccessNotice{
    [[NSNotificationCenter defaultCenter] postNotificationName:@"ThirdShare_Success_Notice" object:nil];
}

//完成分享任务
-(void)executeShareTask{
    
    //[[[CTAPIVisitorManager sharedManger] userVisitor] executeShareTaskWithHUD:NO success:^(id responseObject) {
    //
    //}];
    
}

@end
